package com.wits.qso.service;

import java.util.Iterator;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Component;
import org.springside.modules.persistence.SearchFilter;

import com.wits.platform.common.persistence.HibernatePropertyFilter;
import com.wits.platform.service.BaseService;
import com.wits.qso.dao.MaterialHDao;
import com.wits.qso.dao.impl.MaterialHDaoImpl.Material;
import com.wits.qso.entity.MaterialD;
import com.wits.qso.entity.MaterialH;


@Component
public class MaterialHService extends BaseService<MaterialH, String>
{
	protected static final Logger logger = LoggerFactory.getLogger( MaterialHService.class );
	private MaterialHDao materialDao = null;

	@Autowired
	public void setMaterialDao(MaterialHDao materialDao) {
		this.baseDao = materialDao;
		this.materialDao = materialDao;
	}
	
	public Page<Material> findMaterials(Map<String, Object> params) {
		Map<String, SearchFilter> filters = HibernatePropertyFilter.parse( params );
		String id = null;
		String couName = null;
		String title = null;

		Iterator<String> keyIter = filters.keySet().iterator();
		while (keyIter.hasNext()) {
			String key = keyIter.next();
			String fieldName = filters.get( key ).fieldName;

			if ("id".equals( fieldName )) {
				id = (String) filters.get( key ).value;
			} else if ("couName".equals( fieldName )) {
				couName = (String) filters.get( key ).value;
			} else if ("title".equals( fieldName )) {
				title = (String) filters.get( key ).value;
			}
		}
		
		return new PageImpl( materialDao.findMaterials( id, couName, title ) );
	}
	
	public MaterialH findByName(String name){
		return materialDao.findByName(name);
	}
}
